<?php
/**
 *User :门前一棵柿子树
 *Date:2024/4/14
 *Email:785313152@qq.com
 */

namespace app\index\controller;

use app\admin\model\ProposeClassify;
use app\admin\model\Userlist;
use app\common\controller\IndexBase;
use app\common\model\Leader;
use app\common\model\Manager;
use app\common\model\Work;
use app\index\model\ProposeLog;
use app\index\service\ProposeService;
use think\Session;

class Propose extends IndexBase
{

    public $model;
    public $service;

    protected function _initialize()
    {
        $this->model = new \app\index\model\Propose();
        $this->service = new ProposeService();
        $cate = ProposeClassify::column('name', 'id');
        $status = [
            0 => "已关闭",
            1 => "已发起",
            2 => "审核中",
            3 => "已通过",
            4 => "待分配",
            5 => "待执行",
            6 => "已完成"
        ];
        $this->assign('cate', $cate);
        $this->assign('status', $status);
        parent::_initialize();
    }

    public function index()
    {
        return $this->fetch();
    }

    /**我的建议
     * @return mixed
     */
    public function my()
    {
        $model = input('model', 1);
        $this->assign('model', $model);
        return $this->fetch();
    }

    /**需要我审批的
     * @return mixed
     */
    public function approval()
    {
        $list = $this->service->approvalList();
        $department = cache('department_list');
        $department = array_column($department['department'], 'name', 'id');
        $this->assign('department', $department);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**所有提议
     * @return mixed
     */
    public function all()
    {
        $department = cache('department_list');
        $department = $department['department'];
        $model = input('model', 0);
        $this->assign('model', $model);
        $this->assign('department', $department);
        return $this->fetch();
    }

    public function get_all_list()
    {

        $department = cache('department_list');
        $department = array_column($department['department'], 'name', 'id');

        $list = $this->service->getAllList();
        $this->assign('list', $list);
        $this->assign('department', $department);
        return $this->fetch();
    }

    public function rank()
    {
        $type = input('type', 1);
        $model = input('model',1);
//        个人榜单
        if ($type == 1&&$model==1) {
            $list = $this->model->field('count(id) as count,create_user')
                ->group('create_user')
                ->order('count desc')
                ->limit('10')
                ->select()->each(function ($item){
                    $ifo  = $this->model->where('create_user','=',$item['create_user'])->order('id desc')->find();
                    $item['name'] = $ifo['create_user_info']['name'];
                })->toArray();
        }
        if ($type == 1&&$model==3) {
            $list = $this->model->field('count(id) as count,solution_user_id')
                ->group('solution_user_id')
                ->where('status','=' ,6)
                ->order('count desc')
                ->limit('10')
                ->select()->each(function ($item){
                    $ifo  = $this->model->where('solution_user_id','=',$item['solution_user_id'])->order('id desc')->find();
                    $item['name'] = $ifo['solution_user_info']['name'];
                })->toArray();
        }
//月
        if ($type == 3&&$model==1) {
            $list = $this->model->field('count(id) as count,create_user')
                ->group('create_user')
                ->order('count desc')
                ->whereTime('add_time','month')
                ->limit('10')
                ->select()->each(function ($item){
                    $ifo  = $this->model->where('create_user','=',$item['create_user'])->order('id desc')->find();
                    $item['name'] = $ifo['create_user_info']['name'];
                })->toArray();
        }
        if ($type == 3&&$model==3) {
            $list = $this->model->field('count(id) as count,solution_user_id')
                ->group('solution_user_id')
                ->where('status','=' ,6)
                ->whereTime('add_time','month')
                ->order('count desc')
                ->limit('10')
                ->select()->each(function ($item){
                    $ifo  = $this->model->where('solution_user_id','=',$item['solution_user_id'])->order('id desc')->find();
                    $item['name'] = $ifo['solution_user_info']['name'];
                })->toArray();
        }

//年
        if ($type == 4&&$model==1) {
            $list = $this->model->field('count(id) as count,create_user')
                ->group('create_user')
                ->order('count desc')
                ->whereTime('add_time','year')
                ->limit('10')
                ->select()->each(function ($item){
                    $ifo  = $this->model->where('create_user','=',$item['create_user'])->order('id desc')->find();
                    $item['name'] = $ifo['create_user_info']['name'];
                })->toArray();
        }
        if ($type == 4&&$model==3) {
            $list = $this->model->field('count(id) as count,solution_user_id')
                ->group('solution_user_id')
                ->where('status','=' ,6)
                ->whereTime('add_time','year')
                ->order('count desc')
                ->limit('10')
                ->select()->each(function ($item){
                    $ifo  = $this->model->where('solution_user_id','=',$item['solution_user_id'])->order('id desc')->find();
                    $item['name'] = $ifo['solution_user_info']['name'];
                })->toArray();
        }
        if ($type == 2&&$model==1) {
            $list = $this->model->field('count(id) as count,user_department')
                ->group('user_department')
                ->order('count desc')
                ->limit('10')
                ->select()->each(function ($item){
                    $department =cache('department_list');
                    $department =array_column($department['department'],'name','id');
                    $item['name'] = $department[$item['user_department']];
                })->toArray();
        }
        if ($type == 2&&$model==3) {
            $list = $this->model->field('count(id) as count,solution_department')
                ->group('solution_department')
                ->where('status','=' ,6)
                ->order('count desc')
                ->limit('10')
                ->select()->each(function ($item){
                    $department =cache('department_list');
                    $department =array_column($department['department'],'name','id');
                    $item['name'] = $department[$item['solution_department']];
                })->toArray();
        }
        $this->assign('model',$model);
        $this->assign('type',$type);
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**发布合理化建议页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        //获取提议人所在部门。有多个部门时需要
        $department = $this->getUserDepartmentlist();
        $this->assign('department', $department);
        //分类
        $classify = ProposeClassify::field('id as value,name as text')->order('sort desc')->select()->toArray();
        $this->assign('classify', $classify);
        return $this->fetch();
    }

    public function detail($id = 0)
    {
        $info = $this->model->find($id);
        if (!$info) {
            exit('信息不存在');
        }
        $info = $info->toArray();
        //分类
        $info['propose_type_text'] = ProposeClassify::where('id', '=', $info['propsoe_type'])->value('name');
        $this->assign('rows', $info);
        //下一步操作按钮
        $isAuth = $this->service->getInfoStatus($id);
        $nextText = [
            1 => '去审批',
            2 => '去审批',
            3 => '设定解决部门',
            4 => '分配任务',
            5 => '去完成',
        ];
        $this->assign('next_text', $nextText);
        $this->assign('is_auth', $isAuth['is_auth']);
        $content = $info['status'] == 5 ? "处理" : "审批";
        $nextStatus = '待' . $isAuth['leader'] . $content;
        $this->assign('next_status', $nextStatus);
        //操作日志
        $log = ProposeLog::where('propose_id', '=', $id)->order('add_time desc')->select()->toArray();
        $this->assign('log', $log);
        return $this->fetch();
    }

    public function deal()
    {
        $id = input('id');
        if ($this->request->isAjax()) {
            return $this->service->deal($id);
        }

        $info = $this->model->find($id);
        if (!$info) {
            exit('信息不存在');
        }
        $this->assign('rows', $info->toArray());
        if (in_array($info['status'], [1, 2, 5])) {
            return $this->fetch('deal');
        }
        if ($info['status'] == 3) {
            return $this->fetch('chose_depart');
        }
        if ($info['status'] == 4) {
            return $this->fetch('chose_user');
        }
    }

    public function choose()
    {
        $id = input('id');
        if ($this->request->isAjax()) {
            return $this->service->choose($id);
        }
    }

    public function choose_user()
    {
        $id = input('id');
        if ($this->request->isAjax()) {
            return $this->service->choose_user($id);
        }
    }

    public function deny()
    {
        $id = input('id');
        if ($this->request->isAjax()) {
            return $this->service->deny($id);
        }
    }


    /**发布合理化建议
     * @return void
     */
    public function publish()
    {
        if ($this->request->isAjax()) {
            return $this->service->publish();
        }
    }

    private function getUserDepartmentlist()
    {
        $userDepartment = \session('user_info.department');
        $departmentList = cache('department_list');
        $departmentList = array_column($departmentList['department'], 'name', 'id');
        $return = [];
        foreach ($userDepartment as $value) {
            $return[] = ['value' => $value, 'text' => $departmentList[$value]];
        }
        return $return;
    }

}